"use strict";

$(function(){
	
	document.onkeydown = function(e){ 
	    var ev = document.all ? window.event : e;
	    if(ev.keyCode==13) {
	    	var value=$("#search").val().trim();
	 		var createTime=$("#createTime").val().trim();
	 		var endTime=$("#endTime").val().trim();
			$.ajax({
				type:"post",
				url:"/admin/user/list",
				async:true,
				data:{"condition":value,"createTime":createTime,"endTime":endTime},
				success:function(data){
					$("#mytable").html(data);
				}
			});
	     }
	}
	
	//搜索
 	$("#search-btn").on("click",function(){
 		var value=$("#search").val().trim();
 		var createTime=$("#createTime").val().trim();
 		var endTime=$("#endTime").val().trim();
		$.ajax({
			type:"post",
			url:"/admin/user/list",
			async:true,
			data:{"condition":value,"createTime":createTime,"endTime":endTime},
			success:function(data){
				$("#mytable").html(data);
			}
		});
 	});
 	
 	
 	/*解除微信号*/
 	$("#unbindWeChat").on("click",function(){
 		var userId=$("#wuserId").val();
 		$.ajax({
			type:"post",
			url:"/admin/user/unbindWeChat",
			async:true,
			dataType:'json',
			data:{"userId":userId},
			success:function(data){
				alert(data.data);
				location.reload(); 
			}
		});
 	})
 	
 	
 	/*重置密码*/
 	$("#rePassword").on("click",function(){
 		var userId=$("#puserId").val();
 		$.ajax({
			type:"post",
			url:"/admin/user/resetPassword",
			async:true,
			dataType:'json',
			data:{"userId":userId},
			success:function(data){
				alert("重置密码成功");
			}
		});
 	})
	//日历选择器
	$('#date').daterangepicker({
	    //startDate: "2017-06-08",
	    //endDate: "2017-06-14",
	    timePicker:true,
	    timePicker24Hour:true,
	    timePickerSeconds:true,
	    parentEl:"#date-picker",
	    applyClass:"btn-primary",
	    opens:"left",
	    //本地化配置
	    locale : {  
	    	format: "YYYY-MM-DD HH:MM:SS",
	        applyLabel : '确定',  
	        cancelLabel : '取消',  
	        fromLabel : '起始时间',  
	        toLabel : '结束时间',  
	        customRangeLabel : '自定义',  
	        daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],  
	        monthNames : [ '1月', '2月', '3月', '4月', '5月', '6月',  
	                '7月', '8月', '9月', '10月', '11月', '12月' ],  
	        firstDay : 1  
	    }  
	}, function(start, end, label) {
		  //console.log("New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + ' (predefined range: ' + label + ')");
	});
	
	//绑定日历事件，由日历"确认"按钮触发
	$('#date').on('apply.daterangepicker',function(ev, picker) {
		$("#createTime").val(picker.startDate.format('YYYY-MM-DD'));
		$("#endTime").val(picker.endDate.format('YYYY-MM-DD'));
	});
	
	//确认解绑微信号
	userTable.unbind("#unbind-modal .confirm",function(data){
		//参数data为表格行的id(data-userId)的值
		console.log(data);
		Global.modal("#unbind-modal","hide");
	});
	
	//确认重置密码
	userTable.unbind("#reset-modal .confirm",function(data){
		//参数data为表格行的id(data-userId)的值
		console.log(data);
		Global.modal("#reset-modal","hide");
	});
 	
 	//以创建时间排列,order为0时执行升序排列，为1时执行降序排列
 	$("#create-time").on("click",function(){
 		var order=$(this).attr("data-order");
 		var condition=$("#search").val().trim();
 		var createTime=$("#createTime").val().trim();
 		var endTime=$("#endTime").val().trim();
 		if(order==="0"){
 			//升序
 			$(this).attr("data-order","1");
 			orderSort(condition, createTime, endTime, "createTime", 2);
 		}else{
 			//降序
 			$(this).attr("data-order","0");
 			orderSort(condition, createTime, endTime, "createTime", 1);
 		}
 	});
 	
 	//以最后登录时间排列,order为0时执行升序排列，为1时执行降序排列
 	$("#login-time").on("click",function(){
 		var order=$(this).attr("data-order");
 		var condition=$("#search").val().trim();
 		var createTime=$("#createTime").val().trim();
 		var endTime=$("#endTime").val().trim();
 		if(order==="0"){
 			//升序
 			$(this).attr("data-order","1");
 			orderSort(condition, createTime, endTime, "lastLoginTime", 2);
 		}else{
 			//降序
 			$(this).attr("data-order","0");
 			orderSort(condition, createTime, endTime, "lastLoginTime", 1);
 		}
 	});
 	
 	//排序
 	function orderSort(condition,createTime,endTime,orderValue, orderType){
		$.ajax({
			type:"post",
			url:"/admin/user/list",
			async:true,
			data:{"condition":condition,"createTime":createTime,"endTime":endTime,"orderValue":orderValue,"orderType":orderType},
			success:function(data){
				$("#mytable").html(data);
			}
		});
 	}
 	
 	
 	//分页操作，跳转到某页
 	$("#go-page").on("click",function(){
 		var page=$("#page").val().trim();
 		if(page!="" && page!=null){
 			//do something
 		}
 	});
});

function lockingUser(userId,status){
	status = (status == 1) ?2:1;
		$.ajax({
		type:"post",
		url:"/admin/user/lockingUser",
		async:true,
		dataType:'json',
		data:{"userId":userId,"status":status},
		success:function(data){
			if(status==2) {
				alert("用户锁定成功");
			} else {
				alert("用户解锁成功");
			}
			location.reload();
		}
	});
}

function typeSubmit(type){
	$("#type").val(type);
	var condition=$("#search").val().trim();
	var createTime=$("#createTime").val().trim();
	var endTime=$("#endTime").val().trim();
	$.ajax({
		type:"post",
		url:"/admin/user/list",
		async:true,
		data:{"condition":condition,"createTime":createTime,"endTime":endTime,"type":type},
		success:function(data){
			$("#mytable").html(data);
		}
	});
}
function statusSubmit(isBindWeChat){
	var condition=$("#search").val().trim();
	var type=$("#type").val().trim();
	var createTime=$("#createTime").val().trim();
	var endTime=$("#endTime").val().trim();
	$.ajax({
		type:"post",
		url:"/admin/user/list",
		async:true,
		data:{"condition":condition,"createTime":createTime,"endTime":endTime,"type":type,"isBindWeChat":isBindWeChat},
		success:function(data){
			$("#mytable").html(data);
		}
	});
}
/*重置密码前*/
function resetPassword(userId){
	Global.modal("#reset-modal","show");
	$("#puserId").val(userId);
}
/*解绑微信前*/
function unbind(userId){
	Global.modal("#unbind-modal","show");
	$("#wuserId").val(userId);
}

function showQrcode(userId){
	Global.modal("#qrcode-modal","show");
	var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx243ef1d8389b4e76&redirect_uri=http%3a%2f%2f192.168.1.99%3a8080%2fscanCode%2fauthWechatOpenId&response_type=code&scope=snsapi_userinfo&state="+userId+"#wechat_redirect"
//	var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx243ef1d8389b4e76&redirect_uri=http%3a%2f%2fwww.vchello.cn%2fscanCode%2fauthWechatOpenId&response_type=code&scope=snsapi_userinfo&state="+userId+"#wechat_redirect"
	$("#showqrcode").qrcode({ 
	    render: "canvas", //table方式 
	    width: 250, //宽度 
	    height:250, //高度 
	    text: url //任意内容 
	}); 
}
